<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>站点列表</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

    <script src="/js/axios.min.js"></script>
    <script src="/js/vue.js"></script>
    <script src="/js/qs.min.js"></script>
    <script src="/js/jquery.js"></script>

    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
    <a href="">站点列表</a>
    <div id="app">
        <template>
            <el-table style="width: 100%"
                      :data="sites"
                      ref="siteTable">
                <el-table-column label="id" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.id }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="站点名" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="站点地址" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.address }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="父站点" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.person }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="电话" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.telephone }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="pid" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.pid }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-popconfirm icon-color="red" title="确定删除吗？" @confirm="handleDelete(scope.$index, scope.row)">
                            <el-button size="mini" icon="el-icon-delete" type="danger" slot="reference">删除</el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block" style="margin-top: 10px;">
                <el-pagination @size-change="handleSizeChange"
                               @current-change="handleCurrentChange"
                               :current-page="currentPage"
                               :page-sizes="page_size"
                               :page-size="4"
                               layout="total, sizes, prev, pager, next, jumper"
                               :total="count"
                               :background="back">
                </el-pagination>
            </div>
            <el-button style="margin-top: 10px;" icon="el-icon-plus" size="mini" @click="dialogFormVisible = true">添加站点</el-button>

            <el-dialog title="站点操作" :visible.sync="dialogFormVisible" width="500px" @close="closeDialog()">
                <el-form :model="site">
                    <el-form-item label="站点名">
                        <el-input v-model="site.name" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <el-form :model="site">
                    <el-form-item label="站点地址">
                        <el-input v-model="site.address" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <el-form :model="site">
                    <el-form-item label="父站点">
                        <el-input v-model="site.person" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <el-form :model="site">
                    <el-form-item label="电话">
                        <el-input v-model="site.telephone" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <el-form :model="site">
                    <el-form-item label="pid">
                        <el-input v-model="site.pid" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveSite()">确 定</el-button>
                </div>
            </el-dialog>
        </template>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data(){
            return{
                // 当前页数
                currentPage: 1,
                // 订单总数
                count : 0,
                // 每页的数据
                page_size : [6,3],
                // 当前每页的数据
                pageSize : 6,
                // 分页背景
                back: true,
                sites: [],
                site: {},
                dialogFormVisible: false,
                flag: 0,
            }
        },
        methods:{
            // 加载用户站点
            loadSites(){
                axios.get("/sites/"+this.currentPage+"/"+this.pageSize)
                    .then(res => {
                        this.sites = res.data.data;
                        this.count = res.data.count;
                    })
            },
            // 更新,添加站点
            saveSite() {
                if(this.flag == 0){
                    axios.post("/site", this.site)
                        .then(res => {
                            if(res.data == "ok"){
                                this.loadSites();
                            }
                        });
                }else {
                    axios.put("/site", this.site)
                        .then(res => {
                            if(res.data == "ok"){
                                this.loadSites();
                            }
                        });
                }
                this.dialogFormVisible = false;
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.loadSites();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.loadSites();
            },
            handleEdit(index, row) {
                this.site = row;
                this.flag = 1;
                this.dialogFormVisible = true;
            },
            // 删除站点
            handleDelete(index, row) {
                if(this.sites.length == 1){
                    this.currentPage = this.currentPage - 1;
                }
                axios.delete("/site/" + row.id)
                    .then(res => {
                        if(res.data == "ok"){
                            this.loadSites();
                        }
                    });
            },
            //清空修改角色模态框数据
            closeDialog() {
                this.flag = 0;
                this.site = {};
            },
        },
        mounted(){
            this.loadSites();
        }
    });
</script>
</html>